package com.thundersoft.pickcolor.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import com.thundersoft.pickcolor.MainActivity;
import com.thundersoft.pickcolor.R;
import com.thundersoft.pickcolor.database.DatabaseHelper;
import com.thundersoft.pickcolor.utils.MD5Util;
import com.thundersoft.pickcolor.utils.ShareUtils;
import com.thundersoft.pickcolor.utils.ToastUtils;

/**
 * 登录
 */

public class LoginActivity extends AppCompatActivity {

    private EditText et_login_username,et_login_password;
    private DatabaseHelper mDatabaseHelper;
    private SQLiteDatabase mSQLiteDatabase;
    private String user;
    private String pwd ;
    private int id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_login_username = findViewById(R.id.et_login_username);
        et_login_password = findViewById(R.id.et_login_password);
        mDatabaseHelper = new DatabaseHelper(this,"PickColor.db",
                null,1);
    }

    // 跳转注册页面
    public void toRegister(View view) {
        startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
    }

    // 登录
    public void toLogin(View view) {
        String username = et_login_username.getText().toString().trim();
        String password = et_login_password.getText().toString().trim();
        password = MD5Util.encrypt(password);
        if(username.equals("")||password.equals("")){
            ToastUtils.Info(this,getString(R.string.login_input_null));
        }else if(!querySQLite(username)){
            ToastUtils.Info(this, getString(R.string.login_user_empty));
        }else if(username.equals(user)&&password.equals(pwd)){
            ShareUtils.putBoolean(this,"isLogin",true);
            ToastUtils.Success(this, getString(R.string.login_success));
            ShareUtils.putInt(this,"userId",id);
            ShareUtils.putString(this,"username",username);
            ShareUtils.putString(this,"sex","无");
            ShareUtils.putInt(this,"age",0);
            startActivity(new Intent(LoginActivity.this,MainActivity.class));
            finish();
        }else {
            ToastUtils.Info(this, getString(R.string.login_password_error));
        }
    }

    /**
     * 查询数据库是否有该用户
     * @param username
     * @return
     */
    private boolean querySQLite(String username) {
        mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
        Cursor cursor = null;
        try {
            cursor = mSQLiteDatabase.query("User",null,"name = ?",
                    new String[]{username},null,null,null);
            if(cursor.moveToFirst()){
                // 数据库有该用户
                do{
                    id = cursor.getInt(cursor.getColumnIndex("id"));
                    user = cursor.getString(cursor.getColumnIndex("name"));
                    pwd = cursor.getString(cursor.getColumnIndex("password"));
                }while (cursor.moveToNext());
                return true;
            }else {
                // 数据库没有该用户
                clearText();
                return false;
            }
        }catch (Exception e){
            clearText();
            return false;
        }finally {
            cursor.close();
        }
    }

    private void clearText(){
        id = 0;
        user ="";
        pwd = "";
    }

}
